<?php
/*
 * Created on 2016/10/30
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 /**
  * 初始化连接数据库
  */
  header ( "Content-type:text/html;charset=utf-8" );
 class DB{
 	
 	public  $url = "192.168.137.11";
	public  $user = "root";
	public  $pwd = "123456";
	public  $port = 3306;
	public  $dbname = "dcfq";
	public  $charset = "utf-8";
 	function  __construct(){
 		
    }
    
  	public function queryValue($sql){
  		
 		if(!$sql){
 			return 0;
 		}
    	$result = $this -> queryResult($sql);
    	return $result;
	 }
	 
	public function queryResult($sql){
		$conn = new mysqli($this -> url, $this -> user, $this -> pwd, $this -> dbname, $this -> port);
		$conn -> set_charset("utf8");
		$result = 0;
		if($conn->connect_error){
			$result=0;
		}else{
			$results = $conn->query($sql);
			while ($row = $results->fetch_assoc()) {
		      $result = $row["total"];
		    }
		}
		$conn -> close();
		return $result;
	 }
	 	
	public function insertValue($sql){
		try{
			$mysqlis = new mysqli($this -> url, $this -> user, $this -> pwd, $this -> dbname, intval($this -> port));
			$mysqlis -> set_charset("utf8");
			if($mysqlis->connect_error){
				return 0;
			}else{
				$results = $mysqlis->query($sql);
				$mysqlis->close();
				return $results;
			}
			
		}catch( Exception $e ) {
			return $e;
		}
		
	 }
 	
 	public  function insert_array($data,$table){
 		if(!$table){
 			return 0;
 		}
 		if(!$data){
 			return 0;
 		}
 		$sql = "INSERT INTO " . $table;
 		$sql_fields = array();
 		$sql_values = array();
 		foreach($data AS $key=>$value){
			$sql_fields[] = "".$key."";
			$sql_values[] = "'".$value."'";
 		}
    	$sql.= "(".(implode(",",$sql_fields)).") VALUES(".(implode(",",$sql_values)).")";
    	return $this->insertValue($sql);
 	}
 	
 	public function closeConntion($conn){
 		if($conn){
 			mysqli_close($conn);
 		}
 	}
 	
 	/**
 	 * 获取用户信息
 	 * null 没有该用户
 	 */
 	public function getUserInfo($sql){
 		$conn = new mysqli($this -> url, $this -> user, $this -> pwd, $this -> dbname, $this -> port);
 		$conn -> set_charset("utf8");
 		$result = array();
 		if($conn->connect_error){
			$result=null;
		}else{
			$results = $conn->query($sql);
			while ($row = $results->fetch_assoc()) {
		      $result = $row;
		    }
		}
		$conn -> close();
		return json_encode($result,JSON_UNESCAPED_UNICODE);
 	}
 	
 	/**
 	 * 修改密码
 	 */
 	public function updatePwd($sql){
 		try{
	 		$conn = new mysqli($this -> url, $this -> user, $this -> pwd, $this -> dbname, $this -> port);
	 		$conn -> set_charset("utf8");
	 		$result = array();
	 		if($conn->connect_error){
				$result=null;
			}else{
				$conn->query($sql);
			}
			$conn -> close();
			return 0;
		}catch( Exception $e ) {
			return 1;
		}
 	}
 }
?>
